package com.gy.paidao.util;

import java.util.ArrayList;
import java.util.List;

import kankan.wheel.widget.WheelView;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gy.paidao.R;

/**
 * 封装选择底部弹出的popupwindow
 */
public class SelectPopupUtil {

    private Context context;
    private List<SelectYearWheelAdapter> adapters;// 所有的adapter
    private List<WheelView> views;
    private List<Integer> values;
    private int[] ids;
    private TextView tvSure, tvCancel;
    private LinearLayout llMain;

    public SelectPopupUtil(Context context) {
        this.context = context;
        adapters = new ArrayList<SelectYearWheelAdapter>();
        views = new ArrayList<WheelView>();
        values = new ArrayList<Integer>();
        ids = new int[]{R.id.pop_select_util_wv1, R.id.pop_select_util_wv2,
                R.id.pop_select_util_wv3, R.id.pop_select_util_wv4,
                R.id.pop_select_util_wv5};
    }

    /**
     * 选择底部弹出
     *
     * @param items 弹出几个
     * @param datas 数据源
     */
    public void selectPopup(View parent, int items, List<List<String>> datas,
                            final SelectPopupListener listener) {
        views.clear();
        adapters.clear();
        values.clear();
        View view = LayoutInflater.from(context).inflate(
                R.layout.pop_select_util_layout, null);
        final PopupWindow popupWindow = new PopupWindow(context);
        llMain = (LinearLayout) view.findViewById(R.id.pop_select_util_ll_main);
        tvSure = (TextView) view.findViewById(R.id.pop_select_util_tv_sure);
        tvCancel = (TextView) view.findViewById(R.id.pop_select_util_tv_back);
        tvCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
            }
        });
        tvSure.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
                for (WheelView wv : views) {
                    values.add(wv.getCurrentItem());
                }
                listener.selectPopup(values);
            }
        });
        for (int i = 0; i < 5; i++) {
            WheelView wv = (WheelView) view.findViewById(ids[i]);
            if (i < items) {
                wv.setVisibility(View.VISIBLE);
                SelectYearWheelAdapter adapter = new SelectYearWheelAdapter(
                        context, datas.get(i));
                wv.setWheelBackground(R.color.white);
                wv.setWheelForeground(R.mipmap.ic_add_eat_wheel_foreground);
                wv.setViewAdapter(adapter);
                wv.setCyclic(false);//是否循环显示
                wv.setVisibleItems(5);
                wv.setCurrentItem(0);
//				wv.setCurrentItem(5);

                adapters.add(adapter);
                views.add(wv);
            } else {
                wv.setVisibility(View.GONE);
            }
        }

        popupWindow.setContentView(view);
        popupWindow.setWidth(LayoutParams.FILL_PARENT);
        popupWindow.setHeight(LayoutParams.FILL_PARENT);
        popupWindow.setFocusable(true);

        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x90000000);
        popupWindow.setBackgroundDrawable(dw);
        TranslateAnimation anim = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f);
        anim.setDuration(500);
        llMain.setAnimation(anim);
        // popupWindow.setAnimationStyle(R.style.popupAnimation);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }
}
